package com.example.demomk;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demomk.Adapter.MeasurementAdapter;
import com.example.demomk.Fragment.HomeFragment;
import com.example.demomk.Interface.OnBottomBarListener;
import com.example.demomk.Interface.OnGeneratePDFListener;
import com.example.demomk.Utils.CustomNavBar;
import com.example.demomk.Utils.baseActivity;
import com.example.demomk.ViewModel.MeasurementViewModel;
import com.example.demomk.databinding.ActivityMainBinding;
import com.example.demomk.db.AppDatabase;

import java.util.List;

public class MainActivity extends baseActivity implements OnBottomBarListener {
    private MeasurementAdapter adapter;
    private RecyclerView recyclerView;
    private  String currentTime;
    private ActivityMainBinding binding;
    private MeasurementViewModel viewModel;
    protected CustomNavBar customNavBar;

    @Override
    public void setHomeButtonListener(View.OnClickListener listener) {
        binding.bottomBar.home.setOnClickListener(listener);
    }
    @Override
    public void setManageButtonListener(View.OnClickListener listener) {
        binding.bottomBar.guanli.setOnClickListener(listener);
    }
    @Override
    public void setSearchButtonListener(View.OnClickListener listener) {
        binding.bottomBar.search.setOnClickListener(listener);
    }
    @Override
    public void setPrintButtonListener(View.OnClickListener listener) {
        binding.bottomBar.print.setOnClickListener(listener);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.container), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            // 只设置左右和底部的 padding，避免影响顶部 margin
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom);
            return insets;
        });

        if (savedInstanceState == null) {
            loadFragment(new HomeFragment());
        }

        // 预填充一些测试数据
//        binding.insert.setOnClickListener(v -> {
//
//            int userId = 1;
//            int locationId = 2;
//
//            // 使用子线程进行数据库操作
//            AppDatabase.databaseWriteExecutor.execute(() -> {
//                int count = viewModel.isExist(userId, locationId);
//
//                if (count == 0) {
//                    AvgMeasurement avgMeasurement = new AvgMeasurement(userId, locationId, currentTime, 100);
//                    viewModel.insertAvgMeasurement(avgMeasurement);
//                    runOnUiThread(() -> Toast.makeText(MainActivity.this, "插入成功", Toast.LENGTH_SHORT).show());
//                } else {
//                    runOnUiThread(() -> Toast.makeText(MainActivity.this, "该用户已存在", Toast.LENGTH_SHORT).show());
//                }
//            });
//
//        });


    }
    //切换Fragment
    private void loadFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fragment_container, fragment,"HomeFragment")
                .commit();
    }
    public void setBottomBarguanli(View.OnClickListener listener) {
        binding.bottomBar.guanli.setOnClickListener(listener);
    }
    public void setBottomBarprint(View.OnClickListener listener) {
        binding.bottomBar.print.setOnClickListener(listener);
    }
    //重写接口方法

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(customNavBar != null){
            customNavBar.destroy();
        }
    }
}